﻿
#ifndef ASYNC_NAMED_PIPE_RESULT_H_
#define ASYNC_NAMED_PIPE_RESULT_H_

#include "../utils/event_delegate.h"
#include "../utils/event_adapter.h"
#include "async_event_args.h"

namespace nsshare
{
namespace sys
{
namespace AsyncNamedPipeOp
{
enum Value
{
    kAsyncPipeUnknow,
    kAsyncPipeAccept,
    kAsyncPipeRead,
    kAsyncPipeWrite
};
}

class Event;
class NamedPipeAsyncEventArgs;

typedef utils::EventDelegate<NamedPipeAsyncEventArgs&> NamedPipeAsyncEventDelegate;
typedef utils::EventHandler<NamedPipeAsyncEventArgs&>  NamedPipeAsyncEventHandler;

class NamedPipeAsyncEventArgs : public AsyncEventArgs
{
public:
    NamedPipeAsyncEventArgs();
    NamedPipeAsyncEventArgs(Event & e);

    void SetBuffer(const void * buffer, size_t count);
    void SetBuffer(void * buffer, size_t count);

    void set_completion_delegate(NamedPipeAsyncEventHandler * handler);

    void * data() const;
    size_t count() const;
    uint32_t error() const;
    uint32_t transfered() const;
    AsyncNamedPipeOp::Value last_op() const;

private:
    void OnCompleted(uint32_t error, uint32_t transfered);

private:
    void * data_;
    size_t count_;
    uint32_t error_;
    uint32_t transfered_;
    AsyncNamedPipeOp::Value last_op_;
    NamedPipeAsyncEventDelegate completion_delegate_;

    friend class NamedPipe;
    friend class NamedPipeServer;
    friend class NamedPipeClient;
    friend class NamedPipeRoutines;
};

template <typename Adaptee>
class NamedPipeAsyncEventAdapter
    : public utils::EventAdapter<Adaptee, NamedPipeAsyncEventArgs&>
{
};

}
}

#endif
